/**
 * This software was produced for the U. S. Government
 * under Contract No. FA8721-09-C-0002, and is
 * subject to the Rights in Noncommercial Computer Software
 * and Noncommercial Computer Software Documentation Clause
 * (DFARS) 252.227-7014 (JUN 1995)
 * 
 * (c) 2009 The MITRE Corporation. All Rights Reserved.
 *
 * SPMIF to OPNET Translator
 * Author: ewalters
 * Created: 3:35:51 PM, Aug 13, 2007
 * MITRE Corporation
 */
package com.mitre.spmif2opnet.generation.ir;

import java.util.LinkedList;
import java.util.List;

import com.mitre.spmif2opnet.generation.OpnetGraphConverter;
import com.mitre.spmif2opnet.opnet.network.Attr;

/**
 * Represents the endpoint of a phase, and can be mapped to a logical node
 */
public class Endpoint {
	/**
	 * Represents special properties of a source as per OPNET
	 */
	public enum SpecialPoint {
		ORIGINATING_SOURCE {
			public String toString() {
				return "Originating Source";
			}
		},
		PREV_DESTINATION {
			public String toString() {
				return "Previous Destination";
			}
		},
		PREV_SOURCE {
			public String toString() {
				return "Previous Source";
			}
		},
		NAMED {
			public String toString() {
				return "";
			}
		};
	}

	/**
	 * Name keyed to logical nodes
	 */
	private String _name;
	/**
	 * "Special" OPNET Point designation
	 */
	private SpecialPoint _spoint;
	/**
	 * Traffic spec
	 */
	private double _init_time;
	/**
	 * Job associated with the endpoint
	 */
	private String _job_name;
	/**
	 * Size of the packet
	 */
	private int _packet_size;
	/** 
	 * inter-request time - currently unused
	 */
	private double _inter_time;
	
	/** 
	 * Default constructor
	 * @param name endpoint name
	 */
	public Endpoint(String name) {
		_name = name;
		// NOTE: These are defaults to make sure things come out kosher during
		//  simulation - we should figure out a way to transport via S-PMIF
		_inter_time = 0.01;
		_packet_size = 1024;
	}
	
	/**
	 * Copy constructor
	 * @param e source endpoint
	 */
	public Endpoint(Endpoint e) {
		_name = e._name;
		_spoint = e._spoint;
		_job_name = e._job_name;
		_init_time = e._init_time;
		_packet_size = e._packet_size;
		_inter_time = e._inter_time;
	}
	
	/**
	 * Special point mutator
	 * @param sp the special point
	 */
	public void setPointDesignation(SpecialPoint sp) {
		_spoint = sp;
	}
	
	/**
	 * init time mutator
	 * @param t the time
	 */
	public void setInitTime(double t) {
		_init_time = t;
	}
	
	/**
	 * init time accessor
	 * @return the time
	 */
	public double getInitTime() {
		return _init_time;
	}
	
	/**
	 * Job name mutator
	 * @param jname the name
	 */
	public void setJobName(String jname) {
		_job_name = jname;
	}
	
	/**
	 * Packet size mutator
	 * @param size the size
	 */
	public void setPacketSize(int size) {
		_packet_size = size;
	}

	/**
	 * Endpoint name accessor
	 * @return the name
	 */
	public String getName() {
		return _name;
	}
	
	/**
	 * request time mutator
	 * @param t the time
	 */
	public void setInterrequestTime(double t) {
		_inter_time = t;
	}
	
	/**
	 * Turn the endpoint into XML attributes
	 * @param prefix attribute prefix
	 * @param is this a source endpoint?
	 * @return list of XML attributes
	 */
	public List<Attr> getAttributes(String prefix, boolean is_source) {
		List<Attr> attrs = new LinkedList<Attr>();
		String node1 = is_source ? "Source " : "Destination ";
		String node1tonode2 = is_source ? "Source->Dest " : "Dest->Source ";
		Attr a = new Attr();
		if (_spoint.equals(SpecialPoint.NAMED)) {
			a.setValue(_name);
			a.setSymbolic("false");
		} else {
			a.setValue(_spoint.toString());
			a.setSymbolic("true");
		}
		a.setName(prefix + node1);
		a.setIntended("true");
		attrs.add(a);
		attrs.add(OpnetGraphConverter.makeAttr(prefix + node1tonode2 + "Traffic.count", "1"));
		if (is_source) {
			attrs.add(OpnetGraphConverter.makeAttr(prefix + node1tonode2 + "Traffic [0]. Initialization Time", "constant (" + _init_time + ")"));
			attrs.add(OpnetGraphConverter.makeAttr(prefix + node1tonode2 + "Traffic [0]. Request Count", "constant (1)"));
			attrs.add(OpnetGraphConverter.makeAttr(prefix + node1tonode2 + "Traffic [0]. Interrequest Time", "constant (" + _inter_time +  ")"));
			attrs.add(OpnetGraphConverter.makeAttr(prefix + node1tonode2 + "Traffic [0]. Request Packet Size", "constant (" + _packet_size + ")"));
			attrs.add(OpnetGraphConverter.makeAttr(prefix + node1tonode2 + "Traffic [0]. Packets Per Request ", "constant (1)"));
		}
		attrs.add(OpnetGraphConverter.makeAttr(prefix + node1tonode2 + "Traffic [0]. Server Job Name", _job_name));
		return attrs;
	}

}
 